/**@file   usart.c 
 * @brief
 * 
 * @author SealedGhost
 */
 
 
 
#include "usart.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"

#include "hw_config.h"

#include "led.h"


#include "event_flag.h"

#include "msg_queue.h"

#include "string_util.h"
#include "m26.h"


extern MsgQueue ATMsgQueue;
extern MsgQueue TIMMsgQueue;


uint8_t Usart1_RxBuf[USART1_RX_BUF_SIZE]  = {0};
uint16_t Usart1_RxCnt  = 0;


void initUsart1(uint32_t baudRate)
{
	GPIO_InitTypeDef 	gpioConfig;
	USART_InitTypeDef 	usartConfig;
	
	RCC_APB2PeriphClockCmd(USART1_PORT_CLK | USART1_CLK, ENABLE);	
	
	USART_DeInit(USART1);
	
	gpioConfig.GPIO_Speed		= GPIO_Speed_50MHz;
	gpioConfig.GPIO_Mode		= GPIO_Mode_AF_PP;
	gpioConfig.GPIO_Pin			= USART1_Tx_Pin;
	GPIO_Init(USART1_Port, &gpioConfig);
	
	gpioConfig.GPIO_Mode		= GPIO_Mode_IN_FLOATING;
	gpioConfig.GPIO_Pin			= USART1_Rx_Pin;
	GPIO_Init(USART1_Port, &gpioConfig);
	
	usartConfig.USART_BaudRate						= baudRate;
	usartConfig.USART_HardwareFlowControl			= USART_HardwareFlowControl_None;
	usartConfig.USART_Mode							= USART_Mode_Tx | USART_Mode_Rx;
	usartConfig.USART_Parity 						= USART_Parity_No;
	usartConfig.USART_StopBits						= USART_StopBits_1;
	usartConfig.USART_WordLength					= USART_WordLength_8b;
	USART_Init(USART1, &usartConfig);	

	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);

	USART_Cmd(USART1, ENABLE);
}


void initUsart3(uint32_t baudRate)
{
	GPIO_InitTypeDef 	gpioConfig;
	USART_InitTypeDef 	usartConfig;
	
	RCC_APB2PeriphClockCmd(USART3_PORT_CLK, ENABLE);
	RCC_APB1PeriphClockCmd(USART3_CLK, ENABLE);
	
	USART_DeInit(USART3);
	
	gpioConfig.GPIO_Speed		= GPIO_Speed_50MHz;
	gpioConfig.GPIO_Mode		= GPIO_Mode_AF_PP;
	gpioConfig.GPIO_Pin			= USART3_Tx_Pin;
	GPIO_Init(USART3_Port, &gpioConfig);
	
	gpioConfig.GPIO_Mode		= GPIO_Mode_IN_FLOATING;
	gpioConfig.GPIO_Pin			= USART3_Rx_Pin;
	GPIO_Init(USART3_Port, &gpioConfig);
	
	usartConfig.USART_BaudRate						= baudRate;
	usartConfig.USART_HardwareFlowControl			= USART_HardwareFlowControl_None;
	usartConfig.USART_Mode							= USART_Mode_Tx | USART_Mode_Rx;
	usartConfig.USART_Parity 						= USART_Parity_No;
	usartConfig.USART_StopBits						= USART_StopBits_1;
	usartConfig.USART_WordLength					= USART_WordLength_8b;
	USART_Init(USART3, &usartConfig);	

	//USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
	USART_Cmd(USART3, ENABLE);	
}



void sendByteByUsart(USART_TypeDef* usart, uint8_t val)
{
	usart->DR	= val;
	while( (usart->SR & USART_FLAG_TXE) == 0);
	
}



void sendBytesByUsart(USART_TypeDef* usart, uint8_t* buf, size_t size)
{
	while(size--)
	{
		usart->DR  = *buf++;
		while( (usart->SR & USART_FLAG_TXE) == 0);
		
	}
}



void USART1_IRQHandler()
{
	uint32_t length  = 0;
	static uint16_t RxInx  = 0;
	
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
	{
		Usart1_RxBuf[RxInx++]  = USART_ReceiveData(USART1) & 0xff;
	}
	
	
	if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
	{
		Usart1_RxBuf[RxInx++]  = USART_ReceiveData(USART1) & 0xff;
		Usart1_RxCnt  = RxInx;
		Usart1_RxBuf[RxInx]  = '\0';
		//sendBytesByUsart(USART3, Usart1_RxBuf, RxInx);
		if(PostMessage(&ATMsgQueue, MSG_AT, 0, RxInx, (uint32_t)Usart1_RxBuf) == false){
			printf("Post message failed\r\n");
		}
		
		/*
		if( strBeginWith( (char*)Usart1_RxBuf, "+CREG:") ){
			AT_Echo  result  ;
			int n, stat;
			
			result  = ParseGSMStatus((char*)Usart1_RxBuf, &n, &stat);
			if(result == AT_No_Error){
				PostMessage(&ATMsgQueue ,MSG_AT, GSM_STATE_REPORT, n, stat);
//				printf("Parse gsm :%d,%d\n", n, stat);
			}
			else{
				printf("Parse gsm error:%d\n", result);
			}
		}
		else if(strBeginWith( (char*)Usart1_RxBuf, "+CGREG:") ){
			AT_Echo result;
			int n, stat;
			
			result  = ParseGPRSStatus( (char*)Usart1_RxBuf, &n, &stat);
			if(result == AT_No_Error){
				PostMessage(&ATMsgQueue ,MSG_AT, GPRS_STATE_REPORT, n, stat);
//				printf("Parse gprs :%d,%d\n", n, stat);
			}
			else{
				printf("Parse gprs error:%d\n", result);
			}			
		}
		else if(strBeginWith( (char*)Usart1_RxBuf, "+CPIN: READY")){
			PostMessage(&ATMsgQueue, MSG_AT, SIM_STATE, 1, 0);
		}
		else if(strBeginWith( (char*)Usart1_RxBuf, "RDY" )){
			PostMessage(&ATMsgQueue, MSG_AT, CALL_READY, 0, 0);
		}
//		else if(strBeginWith( (char*)Usart1_RxBuf, "SEND OK" )){
//			PostMessage(&ATMsgQueue, MSG_AT, );
//		}
		else {
			PostMessage(&ATMsgQueue, MSG_AT, OTHER, (uint32_t)RxInx, (uint32_t)Usart1_RxBuf);
		}
		*/
		
//		SetEventFlag(EVENT_FLAG_RX);
		RxInx  = 0;
	}
	
}


void USART3_IRQHandler()
{
	if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
	{
		USART_ClearITPendingBit(USART3, USART_IT_RXNE);
		sendByteByUsart(USART1, USART_ReceiveData(USART3));		
	}
}
